package Dao.Process.Imp;

import Dao.Process.StageTimeDao;
import Entity.Process.StageTime;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StageTimeDaoImp implements StageTimeDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public List<StageTime> queryAllStageTime(Connection conn){
        List<StageTime> stageTimes = new ArrayList<>();
        try{
            String sql = "select start_time,end_time,stage from stage_time";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                String startTime = rs.getString("start_time");
                String endTime = rs.getString("end_time");
                String stage = rs.getString("stage");
                StageTime stageTime = new StageTime(startTime,endTime,stage);
                stageTimes.add(stageTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps,rs);
        }
        return stageTimes;
    }


    @Override
    public List<StageTime> getStageTimeBystage(String stageId, Connection conn){
        List<StageTime> stageTimes = new ArrayList<>();
        String sql = "select start_time,end_time,stage from stage_time where stage = ?";
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,stageId);
            rs = ps.executeQuery();
            while (rs.next()){
                String startTime = rs.getString("start_time");
                String endTime = rs.getString("end_time");
                String stage = rs.getString("stage");
                StageTime stageTime = new StageTime(startTime,endTime,stage);
                stageTimes.add(stageTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps,rs);
        }
        return stageTimes;
    }



    @Override
    public Integer updateStageTime(String startTime, String endTime, String stageId, Connection conn){
        int rsSum = 0;
        try{
            String sql = "update stage_time set start_time = ?, end_time = ? where stage = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,startTime);
            ps.setString(2,endTime);
            ps.setString(3,stageId);
            rsSum = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps,rs);
        }
        return rsSum;
    }
}
